package com.xjc.service;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.xjc.common.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * Description
 * </p>
 *
 * @author xiaojincai
 * @since 2025-06-20
 */
@Slf4j
@Service
public class HttpService {

    private final RetryTemplate retryTemplate = new RetryTemplate();

    @Retryable(
            // 触发重试的异常类型， 请求超时、网络异常
            retryFor = {HttpException.class},
            // 默认重试3次
            maxAttempts = 3,
            // 退避策略 延迟1秒，倍数2
            backoff = @Backoff(delay = 1000, multiplier = 2)
    )
    public <T> T sendRequest(HttpRequest request, TypeReference<T> responseType, String apiDescription) {
        return HttpRequestUtil.sendRequest(request, responseType, apiDescription);
    }

}
